programming4us
           
 
 
Programming

Programming Windows Services with Microsoft Visual Basic 2008 : Services and Polling - Updating the Service Events

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/8/2013 9:12:19 PM

We want to use the new service events properly by modifying the current events. This will allow us to reuse these structured literals quickly.

Modifying Our <OnStart>

We will be modifying our current tutorials.vb thread function code to reflect both the changes in using our resource file and to use our new InstanceId constants.

Listing 1. Modifications to <OnStart> to support the new resource file.
Protected Overrides Sub OnStart(ByVal args() As String)
  ' Add code here to start your service. This method should set things
  ' in motion so your service can do its work.
  Try
    m_WorkerThread = New Thread(AddressOf ThreadFunc)
    m_WorkerThread.Name = My.Resources.ThreadName
    m_WorkerThread.Priority = ThreadPriority.Normal
    m_WorkerThread.Start()
    WriteLogEvent(My.Resources.ServiceStarting, ONSTART_INFO, _
    EventLogEntryType.Information, My.Resources.Source)
  Catch ex As Exception
    'We Catch the Exception
    'to avoid any unhandled errors
    'and we will stop the service if any occur here
    Me.Stop()
  End Try
End Sub

The listing shows that I am now using the My Object to read from the localization resource file.

Modifying <OnStop>

Now let’s update the <OnStop> method to reflect the use of Resources and WriteEventLog call changes. Listing 2 shows the changes.

Listing 2. Modifications to <OnStop> to support the new resource file.
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
  Try
    If (Not m_WorkerThread Is Nothing) Then
        Try
            Me.RequestAdditionalTime(THIRTY_SECONDS)
            m_WorkerThread.Join(TIME_OUT)
            m_ThreadAction.StopThread = True
        Catch ex As Exception
        m_ThreadAction = Nothing
        End Try
    End If
    WriteLogEvent(My.Resources.ServiceStopping, ONSTOP_INFO, _
    EventLogEntryType.Information, My.Resources.Source)
  Catch ex As Exception
  End Try
End Sub

Modifying <OnPause>

As shown in Listing 3, you must once again update <OnPause>.

Listing 3. Modifications to <OnPause> to support the new resource file.
Protected Overrides Sub OnPause()
   Try
       m_ThreadAction.Pause = True
       WriteLogEvent(My.Resources.ServicePausing, ONPAUSE_INFO, _
       EventLogEntryType.Information, My.Resources.Source)
   Catch ex As Exception
    'We Catch the Exception
    'to avoid any unhandled errors
    'since we are pausing and
    'logging an event is what failed
    'we will merely write the output
    'to the debug window
    Debug.WriteLine("Error pausing service: " + ex.ToString())
    Me.Stop()
       'do nothing
   End Try
End Sub

Modifying <OnContinue>

Listing 4 shows how to update the <OnContinue> method.

Listing 4. Modifications to <OnContinue> to support the new resource file.
Protected Overrides Sub OnContinue()
   Try
       m_ThreadAction.Pause = False
       WriteLogEvent(My.Resources.ServiceContinuing, ONCONTINUE_INFO, _
       EventLogEntryType.Information, My.Resources.Source)
   Catch ex As Exception
    'We Catch the Exception
    'to avoid any unhandled errors
    'since we are resuming and
    'logging an event is what failed
    'we will merely write the output
    'to the debug window
    Debug.WriteLine("Error resuming service: " + ex.ToString())
    Me.Stop()
   End Try
End Sub

Other -----------------
- Programming Windows Services with Microsoft Visual Basic 2008 : Services and Polling - Adding a Module File, Adding New Polling Code
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 3) - The Cores View
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 2) - CPU Utilization View, The Threads View
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 1)
- Microsoft Visual Studio 2010 : Reports and Debugging - Using the Parallel Stacks Window
- Microsoft Visual Studio 2010 : Reports and Debugging - Using the Parallel Tasks Window
- Microsoft Visual Studio 2010 : Debugging with Visual Studio 2010 (part 2) - Debugging Threads
- Microsoft Visual Studio 2010 : Debugging with Visual Studio 2010 (part 1) - Live Debugging, Performing Post-Mortem Analysis
- .NET Components : Serialization and Class Hierarchies (part 2) - Manual Base-Class Serialization
- .NET Components : Serialization and Class Hierarchies (part 1) - Custom Serialization and Base Classes
- .NET Components : Custom Serialization (part 2) - Constraining Serialization
- .NET Components : Custom Serialization (part 1) - The ISerializable Interface, Implementing ISerializable
- .NET Components : Serialization and Streams - Serializing Multiple Objects
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 5) - Advanced HTTP Handler Programming
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 4) - Serving Images More Effectively
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 3) - The Picture Viewer Handler
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 2) - An HTTP Handler for Quick Data Reports
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 1) - The IHttpHandler Interface
- Microsoft ASP.NET 3.5 : HTTP Handlers and Modules - Quick Overview of the IIS Extensibility API
- Programming WCF Services : Queued Services - The HTTP Bridge
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us